package com.uniq.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.PreparedStatement;
import com.uniq.db.ConnectionFactory;
import java.util.Iterator;
import java.util.List;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;

/**
 * Servlet implementation class StoreStock
 */
@WebServlet("/StoreStock")
public class StoreStock extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StoreStock() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out=response.getWriter();
		Connection c=ConnectionFactory.getConnection();
		try {
			String supplier=request.getParameter("supplier");
		
			String brand=request.getParameter("brand");
			
			String size=request.getParameter("size");
			
			String color=request.getParameter("color");
			
			String article_no=request.getParameter("article_no");
			
			String cost_price=request.getParameter("cost_price");
			
			String selling_price=request.getParameter("selling_price");
			
			String filePath=getServletContext().getRealPath("/images")+"\\QRCode.png";
		//	String filePath="D:/QRCode.png";
			FileInputStream  inputStream = new FileInputStream(new File(filePath));
			String query="insert into stock(supplier,brand,size,color,article_no,cost_price,selling_price,qrcode) values "
					+ "(?,?,?,?,?,?,?,?)";
			
			PreparedStatement s=c.prepareStatement(query);
			s.setString(1,supplier);
			s.setString(2,brand);
			s.setString(3,size);
			s.setString(4,color);
			s.setString(5,article_no);
			s.setDouble(6,Double.parseDouble(cost_price));
			s.setDouble(7,Double.parseDouble(selling_price));
			s.setBinaryStream(8,inputStream,inputStream.available());
			
			s.executeUpdate();
			new File(filePath).delete();
			out.println("<h1>Stock added successfully</h1>");
			out.println("<a href='ViewStock.jsp'>View Stock</a>");
			out.println("<a href='AddStock.jsp'>Again add Stock details</a>");
			
		}
                catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
         PrintWriter out=response.getWriter();
		Connection c=ConnectionFactory.getConnection();
                DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
diskFileItemFactory.setSizeThreshold(40960); /* the unit is bytes */

 String supplier=new String();
 String brand=new String();
String size=new String();
String color=new String();
String article_no=new String();
String cost_price=new String();
String selling_price=new String();

File repositoryPath = new File("/temp");
diskFileItemFactory.setRepository(repositoryPath);

ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
servletFileUpload.setSizeMax(81920);
try {
                List  items = servletFileUpload.parseRequest(request);
                Iterator iter = items.iterator();
while (iter.hasNext()) {
    FileItem item = (FileItem) iter.next();

    if (item.isFormField()) {


        if(item.getFieldName().equals("supplier"))
        {
         supplier=item.getString();
        }
                if(item.getFieldName().equals("brand"))
        {
         brand=item.getString();
        }
        if(item.getFieldName().equals("size"))
        {
         size=item.getString();
        }    
            if(item.getFieldName().equals("color"))
        {
         color=item.getString();
        }    
        if(item.getFieldName().equals("article_no"))
        {
         article_no=item.getString();
        }    
           if(item.getFieldName().equals("cost_price"))
        {
         cost_price=item.getString();
        }   
               if(item.getFieldName().equals("selling_price"))
        {
         selling_price=item.getString();
        }    
           
                
			//String brand=request.getParameter("brand");
			
		//	String size=request.getParameter("size");
			
			//String color=request.getParameter("color");
			
			//String article_no=request.getParameter("article_no");
			
		//	String cost_price=request.getParameter("cost_price");
			
			//String selling_price=request.getParameter("selling_price");
			
			
                        
      String name = item.getFieldName();//text1
      String value = item.getString();

    } else {
      //  processUploadedFile(item);
    }
    
    
    String filePath=getServletContext().getRealPath("/images")+"\\QRCode.png";
		//	String filePath="D:/QRCode.png";
			FileInputStream  inputStream = new FileInputStream(new File(filePath));
			String query="insert into stock(supplier,brand,size,color,article_no,cost_price,selling_price,qrcode) values "
					+ "(?,?,?,?,?,?,?,?)";
			
			PreparedStatement s=c.prepareStatement(query);
			s.setString(1,supplier);
			s.setString(2,brand);
			s.setString(3,size);
			s.setString(4,color);
			s.setString(5,article_no);
			s.setDouble(6,Double.parseDouble(cost_price));
			s.setDouble(7,Double.parseDouble(selling_price));
			s.setBinaryStream(8,inputStream,inputStream.available());
			
			s.executeUpdate();
			new File(filePath).delete();
			out.println("<h1>Stock added successfully</h1>");
			out.println("<a href='ViewStock.jsp'>View Stock</a>");
			out.println("<a href='AddStock.jsp'>Again add Stock details</a>");
    
}
		
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
            
	}

}
